<template>
  <div class="multi-upload-wrap clearfix">
    <form id="submitMultiImgForm"
      method="post" 
      enctype="multipart/form-data" 
      :action="url">
      <upload-list @delete-image="emitDeleteImage" 
        :images="aImageSrc"
        :width="width"
        :height="height"
        :disabled="disabled">
        <label :class="{disabled: disabled}" :style="{'line-height': `${height-5}px`, 'font-size': `${width * .7}px`}">
          <input type="file"
            name="imgFile"
            :disabled="disabled"
            @change="onChangeFile">
        </label>
      </upload-list>
    </form>
  </div>
</template>

<script>
  import UploadList from './uploadList'

  export default {
    props: {
      imgsrc: [Array], // 图片地址
      url: [String], // 图片上传URL地址
      disabled: {
        type: Boolean,
        default: false
      },
      width: {
        type: Number,
        default: 80
      },
      height: {
        type: Number,
        default: 80
      }
    },
    data() {
      return {
        aImageSrc: this.imgsrc
      }
    },
    methods: {
      onChangeFile(ev) {
        const imgVal = ev.target.value || ''
        if (!/.jpg|.jepg|.png/i.test(imgVal)) {
          this.$toast('图片格式无效，请重新选择')
          return
        }
        $('#submitMultiImgForm').ajaxSubmit(data => {
          const url = JSON.parse(data).url // 获取图片地址
          this.aImageSrc.unshift(url)
          this.$emit('on-upload', this.aImageSrc)
        })
      },
      emitDeleteImage(index) {
        this._deleteImage(index)
      },
      // 删除图片
      _deleteImage(index) {
        this.aImageSrc.splice(index, 1)
      },
      // 判断是否存在
      _isReaptImage(src) {
        if (this.aImageSrc.length === 0) {
          return true
        } else {
          return this.aImageSrc.indexOf(src, 0) < 0
        }
      }
    },
    components: {
      UploadList
    }
  }
</script>

<style lang="scss" scoped>
  .multi-upload-wrap {
    // padding: 10px;
    label {
      position: relative;
      float: left;
      width: 100%;
      height: 100%;
      text-align: center;
      border: 1px dashed #dddee1;
      cursor: pointer;
      &::before {
        content: "+";
        color: #ddd;
      }
      input {
        width: 0;
        height: 0;
        opacity: 0;
      }
    }
  }
</style>